﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Card.Manager
{
    public partial class CityManager : BasePage
    {
        #region
        private string Key
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["k"]))
                {
                    return Request.QueryString["k"].ToUpper();
                }
                return string.Empty;
            }
        }
        #endregion

        #region
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DoInit();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.rptList.ItemCommand += new RepeaterCommandEventHandler(rptList_ItemCommand);
            base.OnInit(e);
        }

        void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (string.Compare(e.CommandName.ToString(), "DELETE") == 0)
            {
                BAF.BLL.LookupValueManager.Delete(CONST.LOOKUP_TYPE.CITY, e.CommandArgument.ToString(), this.Passport);
            }
            else if (string.Compare(e.CommandName.ToString(), "SAVEALL") == 0)
            {
                foreach (RepeaterItem item in this.rptList.Items)
                {
                    if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                    {
                        Label lblCode = item.FindControl("lblCode") as Label;
                        TextBox txtSort = item.FindControl("txtSort") as TextBox;
                        int sort = 0;
                        if (lblCode != null 
                            && txtSort != null 
                            && !string.IsNullOrEmpty(lblCode.Text) 
                            && int.TryParse(txtSort.Text,out sort))
                        {
                            BAF.Entity.LookupValue city = BAF.BLL.LookupValueManager.GetEntity(CONST.LOOKUP_TYPE.CITY, lblCode.Text, this.Passport);
                            city.Sort = sort;
                            BAF.BLL.LookupValueManager.Update(city, this.Passport);
                        }
                    }
                }
            }
            this.DoQuery();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.DoQuery();
        }

        protected void dropProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DoQuery();
        }

        protected void dropKey_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DoQuery();
        }
        #endregion

        #region
        private void DoInit()
        {
            DataTable dtProvince = BAF.BLL.LookupValueManager.GetListByLookUpType(CONST.LOOKUP_TYPE.PROVINCE, this.Passport);
            this.dropProvince.DataSource = dtProvince;
            this.dropProvince.DataTextField = "VALUE";
            this.dropProvince.DataValueField = "LOOKUP_CODE";
            this.dropProvince.DataBind();
            this.dropProvince.Items.Insert(0, new ListItem("---全部---", ""));

            if (!string.IsNullOrEmpty(this.Key))
            {
                BAF.Utility.ControlHelper.SelectItemByValue(this.dropKey, this.Key);
            }

            this.DoQuery();
        }

        private void DoQuery()
        {
            DataTable dtCity = BAF.BLL.LookupValueManager.GetList(string.Format(" {0} ORDER BY SORT,VALUE", this.GetQueryCon()), this.Passport).Tables[0];
            this.rptList.DataSource = dtCity;
            this.rptList.DataBind();
        }

        private string GetQueryCon()
        {
            string result = string.Format(" LOOKUP_TYPE='{0}' ", CONST.LOOKUP_TYPE.CITY);
            if (!string.IsNullOrEmpty(this.dropProvince.SelectedValue))
            {
                result += string.Format(" AND ATTRIBUTE1='{0}'", this.dropProvince.SelectedValue);
            }

            if (!string.IsNullOrEmpty(this.dropKey.SelectedValue))
            {
                result += string.Format(" AND ATTRIBUTE2='{0}'", this.dropKey.SelectedValue);
            }

            return result;
        }
        #endregion
    }
}
